/**
 * 
 * ZaZen Box By lgmp.info
 * 
 * Todo:
 * Auto-download of sample loop
 * Loop generator
 * 
 */


package info.lgmp.zazenbox;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.AlertDialog;

public class zazenbox extends Activity implements OnClickListener{
    
	File filecheck;
	MediaPlayer player;
	Button playerButton;
	Integer var1;
	String path1;
	AlertDialog.Builder alertbox;
	
	public void onClick(View v) {
		if (v.getId() == R.id.play) {
			playPause();
		}
	}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        demoLoad();
        
        playerButton = (Button) this.findViewById(R.id.play);
        playerButton.setText(R.string.stop);
        playerButton.setOnClickListener(this);
        
        demoPlay();
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    	player.pause();
    }
    
    @Override
    public void onStop() {
    	super.onStop();
    	player.stop();
    }
    
    private void demoLoad() {
    	
        dirfilecheck();
    	
    	player = new MediaPlayer();
        player.setLooping(true);
        try {
        	player.setDataSource(path1);
            player.prepare();
        }
        catch (IOException e) { e.printStackTrace(); }
        catch (IllegalArgumentException e) { e.printStackTrace(); }
        catch (IllegalStateException e) { e.printStackTrace(); }
    }
    
    private void dirfilecheck() {
    	filecheck = new File(Environment.getExternalStorageDirectory() + "/zazenbox");
        
    	if(filecheck.exists() && filecheck.isDirectory()) {
    		// load files.
    		var1 = 1;
    		path1 = filecheck + "/bm10" + var1 + ".wav";
    	} else {
    		// create folder, dl sample loop, and instruct user how to add music/loops.
    		filecheck.mkdirs();
    		alertbox = new AlertDialog.Builder(this);
    		alertbox.setMessage("Please put loopable media in zazenbox on your sdcard.");
    		alertbox.setNeutralButton("Ok, I will.", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface arg0, int arg1) {
					Toast.makeText(getApplicationContext(), "Please plug in your device now", Toast.LENGTH_LONG).show();
				}
			});
    	
    		alertbox.show();
    	}
    }
    
    private void demoPause() {
    	player.pause();
    	playerButton.setText(R.string.play);
    }
    
    private void demoStop() {
    	player.stop();
    	playerButton.setText(R.string.play);
    }
    
    
    private void demoPlay() {
    	player.start();
    	playerButton.setText(R.string.stop);
    }
    
    private void playPause() {
    	if(player.isPlaying()) {
    		demoStop();
    		//demoPause();
    		//player.release();
    		var1++;
			path1 = filecheck + "/bm10" + var1 + ".wav";
			/*try {
        		player.setDataSource(path1);
            	player.prepare();
			}
			catch (IOException e) { e.printStackTrace(); }
        	catch (IllegalArgumentException e) { e.printStackTrace(); }
        	catch (IllegalStateException e) { e.printStackTrace(); }*/
        	//player.start();
        	//demoPlay();
    	} else {
    		//do stuff
    		demoPlay();
    	}
    }
    
}